namespace Grep.SharePoint.Lib.Extensions
{
    using System.Collections.Generic;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Utilities;
    using Microsoft.SharePoint.WebControls;

    public static class SubMenuTemplateExtensions
    {
        public static MenuItemTemplate AddMenuItem(this SubMenuTemplate self, string id, string displayName,
                                                   string imageUrl, string description, string navigateUrl,
                                                   string onClickScript)
        {
            return self.AddMenuItem(id, displayName, imageUrl, description, navigateUrl, onClickScript,
                                    SPBasePermissions.EmptyMask, PermissionContext.CurrentSite);
        }

        public static MenuItemTemplate AddMenuItem(this SubMenuTemplate self, string id, string displayName,
                                                   string imageUrl, string description, string navigateUrl,
                                                   string onClickScript, SPBasePermissions permissions,
                                                   PermissionContext context)
        {
            MenuItemTemplate child = CreateMenuItem(id, displayName, description, permissions, context, imageUrl,
                                                    navigateUrl, onClickScript);
            self.Controls.Add(child);
            return child;
        }

        public static MenuItemTemplate AddMenuItemAt(this SubMenuTemplate self, int index, string id, string displayName,
                                                     string imageUrl, string description, string navigateUrl,
                                                     string onClickScript)
        {
            return self.AddMenuItemAt(index, id, displayName, imageUrl, description, navigateUrl, onClickScript,
                                      SPBasePermissions.EmptyMask, PermissionContext.CurrentSite);
        }

        public static MenuItemTemplate AddMenuItemAt(this SubMenuTemplate self, int index, string id, string displayName,
                                                     string imageUrl, string description, string navigateUrl,
                                                     string onClickScript, SPBasePermissions permissions,
                                                     PermissionContext context)
        {
            MenuItemTemplate child = CreateMenuItem(id, displayName, description, permissions, context, imageUrl,
                                                    navigateUrl, onClickScript);
            self.Controls.AddAt(index, child);
            return child;
        }

        public static SubMenuTemplate AddSubMenu(this SubMenuTemplate self, string id, string displayName,
                                                 string imageUrl, string description)
        {
            SubMenuTemplate child = CreateSubMenu(id, displayName, imageUrl, description);
            self.Controls.Add(child);
            return child;
        }

        public static SubMenuTemplate AddSubMenuAt(this SubMenuTemplate self, int index, string id, string displayName,
                                                   string imageUrl, string description)
        {
            SubMenuTemplate child = CreateSubMenu(id, displayName, imageUrl, description);
            self.Controls.AddAt(index, child);
            return child;
        }

        public static MenuItemTemplate CreateMenuItem(string id, string displayName, string description,
                                                      SPBasePermissions permissions, PermissionContext context,
                                                      string imageUrl, string navigateUrl, string onClickScript)
        {
            var child = new MenuItemTemplate();
            child.ID = id;
            child.Text = displayName;
            child.Description = description;
            child.Permissions = permissions;
            child.PermissionContext = context;
            if (string.IsNullOrEmpty(imageUrl))
            {
                imageUrl = "/_layouts/images/MenuNewItem.gif";
                child.ImageUrl = imageUrl;
            }
            else
            {
                if (imageUrl.IndexOf("/") == -1)
                {
                    imageUrl = "/_layouts/images/" + imageUrl;
                }
                child.ImageUrl = imageUrl;
            }
            if (string.IsNullOrEmpty(navigateUrl))
            {
                child.ClientOnClickScript = onClickScript;
            }
            else
            {
                child.ClientOnClickNavigateUrl = navigateUrl;
            }
            return child;
        }

        public static SubMenuTemplate CreateSubMenu(string id, string displayName,
                                                    string imageUrl, string description)
        {
            var child = new SubMenuTemplate();
            child.ID = id;
            child.Text = displayName;
            child.Description = description;
            if (string.IsNullOrEmpty(imageUrl))
            {
                imageUrl = "/_layouts/images/MenuNewItem.gif";
                child.ImageUrl = imageUrl;
            }
            else
            {
                if (imageUrl.IndexOf("/") == -1)
                {
                    imageUrl = "/_layouts/images/" + imageUrl;
                }
                child.ImageUrl = imageUrl;
            }
            return child;
        }

        public static IEnumerable<MenuItemTemplate> EnumMenuItems(this SubMenuTemplate self)
        {
            foreach (object item in self.Controls)
            {
                if (item is MenuItemTemplate)
                {
                    yield return item as MenuItemTemplate;
                }
            }
        }

        public static IEnumerable<SubMenuTemplate> EnumSubMenus(this SubMenuTemplate self)
        {
            foreach (object item in self.Controls)
            {
                if (item is SubMenuTemplate)
                {
                    yield return item as SubMenuTemplate;
                }
            }
        }
    }
}